package com.enjoyor.soa.traffic.core.efoms.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.enjoyor.soa.traffic.core.efoms.domain.DicLabelInfo;
import com.enjoyor.soa.traffic.core.efoms.domain.PortaritInfo;
import com.enjoyor.soa.traffic.core.efoms.mapper.IAssetInfoMapper;
import com.enjoyor.soa.traffic.core.efoms.service.IAssetInfoService;
import com.enjoyor.soa.traffic.util.pojo.ResultPojo;

@Service
@Transactional
@SuppressWarnings("rawtypes")
public class AssetInfoService implements IAssetInfoService {

	@Autowired
	private IAssetInfoMapper assetInfoMapper;

	/***
	 * 根据输入的查询时间范围查询故障次数的饼形图 beginTime：开始时间 endTime：结束时间； assentId：资产编号
	 * typeCode：类型编号
	 */
	public ResultPojo getDevFaultPie(String beginTime, String endTime, String assentId, String typeCode) {
		// TODO Auto-generated method stub
		Map<String, Object> map = this.getSelectMap(beginTime, endTime, assentId, typeCode, null);
		ResultPojo resultPojo = new ResultPojo();
		try {
			List list = this.assetInfoMapper.getDevFaultPie(map);			
			resultPojo.setResultList(list);
		} catch (Exception e) {
			// TODO: handle exception
			throw e;
		}
		return resultPojo;
	}

	/***
	 * 根据输入的查询时间范围查询故障次数折线图 beginTime：开始时间 endTime：结束时间； assentId：资产编号
	 * typeCode：类型编号
	 */
	public ResultPojo getDevFaultLine(String beginTime, String endTime, String assentId, String typeCode) {
		// TODO Auto-generated method stub
		Map<String, Object> map = this.getSelectMap(beginTime, endTime, assentId, typeCode, null);
		ResultPojo resultPojo = new ResultPojo();
		try {
			List list = this.assetInfoMapper.getDevFaultLine(map);		
			resultPojo.setResultList(list);
		} catch (Exception e) {
			// TODO: handle exception
			throw e;
		}
		return resultPojo;
	}

	/***
	 * 根据输入的查询时间范围查询巡检次数折线图 beginTime：开始时间 endTime：结束时间； assentId：资产编号
	 * typeCode：类型编号
	 */
	public ResultPojo getDevCheckTimesLine(String beginTime, String endTime, String assentId, String typeCode,
			String checkResultCode, String searchFlag) {
		// TODO Auto-generated method stub
		ResultPojo resultPojo = new ResultPojo();
		Map<String, Object> map = this.getSelectMap(beginTime, endTime, assentId, typeCode, searchFlag);
		map.put("checkResultCode", checkResultCode);
		try {
			List list = this.assetInfoMapper.getDevCheckTimesLine(map);
			resultPojo.setResultList(list);
		} catch (Exception e) {
			// TODO: handle exception
			throw e;
		}
		return resultPojo;
	}

	/***
	 * 根据输入的条件查询报警类型的饼形图 beginTime：开始时间 endTime：结束时间； assentId：资产编号
	 * typeCode：类型编号
	 */
	public ResultPojo getDevWarnTypePie(String beginTime, String endTime, String assentId, String typeCode,
			String warnLevelCode) {
		// TODO Auto-generated method stub
		Map<String, Object> map = this.getSelectMap(beginTime, endTime, assentId, typeCode, null);
		map.put("warnLevelCode", warnLevelCode);
		ResultPojo resultPojo = new ResultPojo();
		try {
			List list = this.assetInfoMapper.getDevWarnTypePie(map);			
			resultPojo.setResultList(list);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return resultPojo;
	}

	/***
	 * 根据输入的条件查询报警类型折线图 beginTime：开始时间 endTime：结束时间； assentId：资产编号 typeCode：类型编号
	 */
	public ResultPojo getDevWranTimesLine(String beginTime, String endTime, String assentId, String typeCode,
			String warnLevelCode, String searchFlag) {
		// TODO Auto-generated method stub
		Map<String, Object> map = this.getSelectMap(beginTime, endTime, assentId, typeCode, searchFlag);
		map.put("warnLevelCode", warnLevelCode);
		ResultPojo resultPojo = new ResultPojo();
		try {
			List list = this.assetInfoMapper.getDevWranTimesLine(map);			
			resultPojo.setResultList(list);
		} catch (Exception e) {
			// TODO: handle exception
			throw e;
		}
		return resultPojo;
	}

	/***
	 * 获取设备设施画像； assentId：资产编号； typeCode：资产类型编号；
	 */
	public ResultPojo getPortRait(String assentId, String typeCode) {
		Map<String, Object> map = this.getSelectMap(null, null, assentId, typeCode, null);
		ResultPojo resultPojo = new ResultPojo();
		try {
			List<PortaritInfo> listPortaritInfo = this.assetInfoMapper.getPortRait(map);
			Map<String, Object> listResult = this.getPortary(listPortaritInfo);
			resultPojo.setResultList(listResult);			
		} catch (Exception e) {
			// TODO: handle exception
			throw e;
		}
		return resultPojo;
	}

	// -----------------------------私有方法开始----------------------------------//
	private Map<String, Object> getSelectMap(String beginTime, String endTime, String assentId, String typeCode,
			String searchFlag) {
		Map<String, Object> map = new HashMap<>();
		map.put("beginTime", beginTime);
		map.put("endTime", endTime);
		map.put("assentId", assentId);
		map.put("typeCode", typeCode);
		map.put("searchFlag", searchFlag);
		return map;
	}

	/***
	 * 画像业务处理；
	 */
	private Map<String, Object> getPortary(List<PortaritInfo> list) {
		//List resultList = new ArrayList<>();
		Map<String, Object> mapPortary = new HashMap<String, Object>();
		// 只有唯一对象信息；
		for (int i = 0; i < list.size(); i++) {
			// 对象属性按照指定格式拆分成
			String use = list.get(i).getPortRarit();
			String arrayss[] = use.split(",");
			List<String> stringB = Arrays.asList(arrayss);
			for (int j = 0; j < stringB.size(); j++) {
				String A = stringB.get(j);
				A = A.replace("[", "").replace("]", "");
				String[] teStrings = A.split("\\|");
				String e1 = teStrings[0];
				String e2 = teStrings[1];
				String e3 = teStrings[2];
				// 判断度量表示，如果度量表示==1；则匹配字段；若度量表示==0；则不匹配字典；
				Map<String, Object> hashMap = new HashMap<String, Object>();
				if (e2.equals("0")) {
					hashMap.put("labelType", e1);
					List<DicLabelInfo> listDicLabelInfo = this.assetInfoMapper.getDicLabel(hashMap);
					if(null != listDicLabelInfo && listDicLabelInfo.size() >0) {
						Map<String, Object> resultMap = new HashMap<String, Object>();
						String portaryName = listDicLabelInfo.get(0).getLabelTypeName();
						//String result = listDicLabelInfo.get(0).getLabelTypeName() + ":" + e3;
						resultMap.put("portaryName", portaryName);
						resultMap.put("portaryNum", e3);
						mapPortary.put(e1, resultMap);
						//resultList.add(resultMap);
					}
					
				} else {
					hashMap.put("labelType", e1);
					hashMap.put("measure", e3);
					List<DicLabelInfo> listDicLabelInfo = this.assetInfoMapper.getDicLabel(hashMap);
					if(null != listDicLabelInfo && listDicLabelInfo.size() >0) {
						Map<String, Object> resultMap = new HashMap<String, Object>();
						String portaryName = listDicLabelInfo.get(0).getMeasureMeam();
						//String result = dicLabelInfo.getLabelTypeName() + ":" + dicLabelInfo.getMeasureMeam() + "(" + e3 + ")";
						resultMap.put("portaryName", portaryName);
						resultMap.put("portaryNum", e3);
						mapPortary.put(e1, resultMap);
					}
				}
			}
		}
		return mapPortary;
	}

}
